# src/crud/sys_entity_i18n.py
from typing import List, Optional
from sqlalchemy.orm import Session
from src.models import sys_entity_i18n as models
from src.schemas import sys_entity_i18n as schemas
from src.schemas import request_model
from src.core import orm_curd
from src.schemas.login_manager import AuthManager


# ---------------------- 单条新增 ----------------------
def create_entity_i18n(db: Session,
                       auth: AuthManager,
                       i18n: schemas.SysEntityI18nCreate) -> dict:
    """新增单条国际化翻译"""
    return orm_curd.Insert(
        db,
        auth,
        models.SysEntityI18n,
        schemas.SysEntityI18n,
        "实体国际化",
        [i18n.dict()],
        unique_cols=["api_id", "entity_type", "entity_id", "language"],  # 业务唯一键
        skip_duplicates=True
    )


# ---------------------- 批量新增 ----------------------
def create_entity_i18ns(db: Session,
                        auth: AuthManager,
                        i18ns: List[schemas.SysEntityI18nCreate]) -> dict:
    """批量新增国际化翻译"""
    return orm_curd.Insert(
        db,
        auth,
        models.SysEntityI18n,
        schemas.SysEntityI18n,
        "实体国际化",
        i18ns,
        unique_cols=["api_id", "entity_type", "entity_id", "language"],
        skip_duplicates=True
    )


# ---------------------- 分页查询 ----------------------
def get_entity_i18ns(db: Session,
                     auth: AuthManager,
                     page: int = 0,
                     page_size: int = 100,
                     FilterModel: Optional[request_model.QueryModel] = None,
                     id: Optional[str] = None) -> dict:
    """分页查询国际化列表"""
    return orm_curd.query_with_page(
        db,
        auth,
        models.SysEntityI18n,
        schemas.SysEntityI18n,
        id=id,
        filter_model=FilterModel,
        page=page,
        page_size=page_size
    )


# ---------------------- 单条更新 ----------------------
def update_entity_i18n(db: Session,
                       auth: AuthManager,
                       i18n_id: str,
                       i18n: schemas.SysEntityI18n) -> schemas.SysEntityI18n:
    """更新单条国际化翻译"""
    update_model = request_model.UpdateModel([
        request_model.UpdateItem(
            pk_names=["id"],
            pk_values=[i18n_id],
            data=i18n.dict(exclude_unset=True)
        )
    ])
    return orm_curd.update(
        db,
        auth,
        models.SysEntityI18n,
        update_model
    )


# ---------------------- 批量更新 ----------------------
def update_entity_i18ns(db: Session,
                        auth: AuthManager,
                        updateModel: request_model.UpdateModel) -> dict:
    """批量更新国际化翻译"""
    return orm_curd.update(
        db,
        auth,
        models.SysEntityI18n,
        updateModel
    )


# ---------------------- 删除 ----------------------
def delete_entity_i18ns(db: Session,
                        auth: AuthManager,
                        deleteModel: request_model.DeleteModel) -> dict:
    """删除国际化翻译（硬删除）"""
    return orm_curd.delete(
        db=db,
        auth=auth,
        model_cls=models.SysEntityI18n,
        delete_obj=deleteModel
    )
